package com.yanqu.road.server.cmd.chefduel.process3;

import com.yanqu.road.entity.activity.chefduel.CrossChefDuel2Rank;
import com.yanqu.road.entity.activity.chefduel.CrossChefDuel2RankListModel;
import com.yanqu.road.entity.activity.chefduel.process2.ChefDuelUpUser;
import com.yanqu.road.entity.activity.chefduel.rank.CampRank;
import com.yanqu.road.entity.activity.chefduel.rank.CampRankListModel;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.chefduel.ChefDuelProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.chefduel.CrossChefDuel3;
import com.yanqu.road.server.manager.chefduel.CrossChefDuelMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.LinkedList;

@Cmd(code = Protocol.C_CHEF_DUEL3_CAMP_RANK_LIST, desc = "阵营排行")
public class CrossChefDuel3CampRankListCmd extends CrossCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ChefDuelProto.ChefDuel2CampRankListReqMsg reqMsg = ChefDuelProto.ChefDuel2CampRankListReqMsg.parseFrom(packet.getBytes());

        ChefDuelProto.ChefDuel2CampRankListRespMsg.Builder clientMsg = ChefDuelProto.ChefDuel2CampRankListRespMsg.newBuilder();
        clientMsg.setRet(0);
        clientMsg.setMyCampId(-1);
        clientMsg.setMyRank(-1);
        clientMsg.setIsGetReward(true);
        CrossChefDuel3 crossChefDuel3 = CrossChefDuelMgr.getCrossChefDuel3IfNull(reqMsg.getActivityId());
        if(crossChefDuel3 != null){
            ChefDuelUpUser cookBoyUpUser = crossChefDuel3.getChefDuelMap().get(packet.getPlayerId());
            if(cookBoyUpUser != null && cookBoyUpUser.getSupportCampId() > 0){
                clientMsg.setMyCampId(cookBoyUpUser.getSupportCampId());
                clientMsg.setIsGetReward(cookBoyUpUser.getCampRewardGet() == 1);
            }
            CampRankListModel listModel = crossChefDuel3.getCampRankListModel();
            LinkedList<CampRank> rankList = listModel.getRankList();
            for (int i = 0; i < rankList.size(); i++) {
                CampRank entry = rankList.get(i);
                if(clientMsg.getMyCampId() == entry.getId()){
                    clientMsg.setMyRank(i+1);
                }
            }
            for (int c = 1; c <= 2; c++) {
                for (int k = 0; k < rankList.size(); k++) {
                    CampRank campRank = rankList.get(k);
                    if(campRank.getId() == c){
                        ChefDuelProto.ChefDuel2CampRankTemp.Builder temp = ChefDuelProto.ChefDuel2CampRankTemp.newBuilder();
                        temp.setCampId(campRank.getId());
                        temp.setScore(campRank.getValue());

                        CrossChefDuel2RankListModel model = crossChefDuel3.getCampUserRankMap().get(campRank.getId());
                        if(model != null){
                            for (CrossChefDuel2Rank rank : model.getRankList()) {
                                ChefDuelProto.ChefDuel2CampUserRankTemp.Builder userTemp = ChefDuelProto.ChefDuel2CampUserRankTemp.newBuilder();
                                userTemp.setUserId(rank.getId());
                                userTemp.setScore(rank.getValue());
                                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(rank.getId());
                                if(userBaseInfo != null){
                                    userTemp.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
                                    temp.addUser(userTemp);
                                }
                            }
                        }
                        clientMsg.addRank(temp);
                    }
                }
            }
        }
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_CHEF_DUEL3_CAMP_RANK_LIST, clientMsg));
    }

    @Override
    public void sendData() {

    }
}
